package cn.dnui_zzh807.dnuifood_16110100807.SomethingElse;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_zzh807.dnuifood_16110100807.bean.ResultBean;
import cn.dnui_zzh807.dnuifood_16110100807.model.FoodModel;
import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;

public class Purchase extends AppCompatActivity implements OnRetrofitListener<ResultBean> {
     private String user_id,food_id,price;
     private EditText num,sum,suggesttime,address;
     private FloatingActionButton add;
     private Button purchase_btn;
     private String num1,sum1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        user_id= getIntent().getStringExtra("user_id");
        food_id=getIntent().getStringExtra("food_id");
        price=getIntent().getStringExtra("price");

        initview();
        initEvents();

    }

    private void initview(){
            add=findViewById(R.id.add_food);
            num=findViewById(R.id.food_num);
            sum=findViewById(R.id.food_sum);
            suggesttime=findViewById(R.id.food_suggesttime);
            address=findViewById(R.id.food_address);
            purchase_btn=findViewById(R.id.purchase_food);
            num1=num.getText().toString();
            sum.setText(price);

    }

    private void initEvents(){
             add.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {

                 }
             });

             purchase_btn.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     initData();
                 }
             });

    }
    private void initData(){

       FoodModel foodModel = new FoodModel();
       foodModel.insertOrder(user_id,food_id,num.getText().toString().trim(),sum.getText().toString().trim(),suggesttime.getText().toString().trim(),address.getText().toString().trim(),this);
   }

    @Override
    public void onSuccess(ResultBean result, int flag) {
             if (result.getSuccess().equals("1")){
                 Toast.makeText(Purchase.this,"购买成功",Toast.LENGTH_SHORT).show();
                 finish();
             }
    }

    @Override
    public void onFail() {
        Toast.makeText(Purchase.this,"购买失败",Toast.LENGTH_SHORT).show();
        finish();
    }
}
